(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.patchTemplate.createVerifyController', createVerifyController);
	createVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'patchTemplateService' ];
	function createVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, patchTemplateService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		var log = $scope.loggerFactory.getLogger("patchTemplateCreateVerify");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.patchTemplate = {};
		ctrl.patchTemplateOld = {};
		
		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.dPatchTplParms.tableConfigs.table);
			ctrl.innerGridOptions.data = 'vm.patchTemplate.dPatchTplParms';
			ctrl.innerGridOptions.enableOperation = true;
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			
			ctrl.parmTypeOptions = parmTypeOptions().data;

			authService.createVerify(params).then(function(response) {
				patchTemplateService.initOptions(ctrl).then(function(result){
					if (response.data && response.data.model && response.data.task) {
						ctrl.patchTemplate = response.data.model;
						ctrl.patchTemplate.taskId = response.data.task.taskId;
						ctrl.patchTemplate.dPatchTplParms = ctrl.patchTemplate.dPatchTplParms || [];	
					}
				});
			});
		};
		
		function parmTypeOptions() {
			return {
				"data" : [ {
					"label" : "Date",
					"value" : "java.sql.Date"
				},{
					"value" : "java.lang.Double",
					"label" : "Double"
				}, {
					"value" : "java.lang.Float",
					"label" : "Float"
				} , {
					"value" : "java.lang.Integer",
					"label" : "Integer"
				} , {
					"value" : "java.lang.Long",
					"label" : "Long"
				} , {
					"value" : "java.lang.Short",
					"label" : "Short"
				} , {
					"value" : "java.lang.String",
					"label" : "String"
				} , {
					"value" : "java.sql.Timestamp",
					"label" : "Timestamp"
				}]
			};
		}

		ctrl.createVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.patchTemplate.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createVerifyApprove(ctrl.patchTemplate).then(approveSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.createVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {		
					'ok' : function(comments) {
						ctrl.patchTemplate.comments = comments;
						authService.createVerifyReject(ctrl.patchTemplate).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
				modalService.openAcknowledgeDialogs({
					'messages' : [ 'title.dialog.successful.approve' ],
					'btnCallback' : {
						'close' : function() {
							$scope.go('cda.workflow.wfSetup.list');
						}
					},
					'redirections' : [ {
						'name' : 'title.infobar.workflow.list',
						'state' : 'cda.workflow.wfSetup.list',
						'params' : {}
					}, {
						'name' : 'title.infobar.patchtemplate.list',
						'state' : 'cda.patchTemplate.list',
						'params' : {}
					}]
				});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.patchtemplate.list',
					'state' : 'cda.patchTemplate.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);